/**
 * @description: 定义班级 service
 * @version: 0.0.1
 * @author: z..
 * @Date: 2023-06-12 18:16:39
 */

import { Provide } from '@midwayjs/core';
import { BrandConfig } from './brand_config.entity';

import { InjectRepository, InjectDataSource } from '@midwayjs/sequelize';
import { Repository, Sequelize } from 'sequelize-typescript';

@Provide()
export class BrandConfigModel {

  @InjectRepository(BrandConfig)
  BrandConfigRepository: Repository<BrandConfig>;

  @InjectDataSource()
  SequelizeQuery: Sequelize

  /**
   * 获取所有品牌列表
   * @returns 
   */
  async getAllBrandsPage(): Promise<any> {
    return await this.BrandConfigRepository.findAll();
  };

  /**
   * 根据id查询品牌信息
   * @param {*} id 
   * @returns 
   */
  async getDataById(id: number): Promise<object> {
    return await this.BrandConfigRepository.findAll({ where: { id: id } });
  };

  /**
    * 根据id更新品牌数据
    * @param {*} param0.id id
    * @param {*} param0.brandName 品牌名称
    * @param {*} param0.brandImg   品牌logo
    * @param {*} param0.alarmConfig    钉钉告警群机器人配置
    * @returns 
    */
  async updateDataById({ id, brandName, brandImg, alarmConfig }: { id: number, brandName: string, brandImg: string, alarmConfig: string }): Promise<any> {
    return await this.BrandConfigRepository.update(
      {
        brand_name: brandName,
        brand_img: brandImg,
        alarm_config: alarmConfig,
        updated_at: new Date()
      },
      {
        where: { id: id },
      }
    );
  }

  /**
    * 新增品牌数据
    * @param {*} param0.brandName 品牌名称
    * @param {*} param0.brandImg   品牌logo
    * @param {*} param0.alarmConfig    钉钉告警群机器人配置
    * @returns 
    */
  async insertDataInfo({ brandName, brandImg, alarmConfig }: { brandName: string, brandImg: string, alarmConfig: string }): Promise<any> {
    return await this.BrandConfigRepository.create({
      brand_name: brandName,
      brand_img: brandImg,
      alarm_config: alarmConfig
    })
  }

  /**
    * 删除品牌信息
    * @param {*} id
    * @returns 
    */
  async deleteDataById(id: number): Promise<any> {
    return await this.BrandConfigRepository.destroy({ where: { id: id } })
  }

  /**
     * 分页查询获取总条数
     * @param {*} searchKey   搜索信息
     * @returns 
     */
  async getTotalNum(searchKey: any): Promise<any> {
    let sql = `SELECT count(1) as num from brand_config;`;
    if (searchKey) {
      sql = `SELECT count(1) AS num FROM brand_config WHERE brand_name LIKE '%${searchKey}%';`;
    }
    const [results] = await this.SequelizeQuery.query(sql);
    return results;
  }

  /**
   * 分页查询获取列表
   * @param {*} pageNum 
   * @param {*} pageSize 
   * @param {*} searchKey 
   * @returns 
   */
  async getDataByPage(pageNum: number, pageSize: number, searchKey: any) {
    const _limit = (pageNum - 1) * pageSize;
    let sql = `SELECT id,brand_name,brand_img,user_id,created_at,updated_at,alarm_config FROM brand_config ORDER BY created_at DESC LIMIT ${_limit},${pageSize};`;
    if (searchKey) {
      sql = `SELECT id,brand_name,brand_img,user_id,created_at,updated_at,alarm_config FROM brand_config WHERE brand_name LIKE '%${searchKey}%' ORDER BY created_at DESC LIMIT ${_limit},${pageSize};`;
    }
    const [results] = await this.SequelizeQuery.query(sql);
    return results;
  }

}